package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController{

 @Autowired
 private UserService userService;

// @PostMapping("/addUser")
// public String addUser(@RequestBody User user){
//  System.out.println(user);
//  userService.addUser(user);
//  return "新增成功";
// }

 @PostMapping("/addUserForm")
 public String addUser(User user){
  System.out.println(user);
  userService.addUser(user);
  return "新增成功";
 }

 @GetMapping("/getUser")
 public List<User> getUser(){
  List<User> userList=userService.getUser();
  return userList;
 }

 @GetMapping("/getUserById")
 public User getUserById(Integer id){
  User user=userService.getUserById(id);
  return user;
 }

 //restful风格传参
 @GetMapping("/user/{id}")
 public User getUser(@PathVariable Integer id){
  return userService.getUserById(id);
 }

 //restful风格传参
 @DeleteMapping("/user/{id}")
 public void deleteUser(@PathVariable Integer id){
  userService.deleteUse(id);
  System.out.println("删除成功");
 }

@PostMapping("/user/{name]/{age}/{sex}")
 public void addUserRest(User user){
  userService.addUser(user);
}


// @PostMapping("/addUser")
// public String addUser(@RequestBody User user){
//  userService.addUser(user);
//  return "新增成功";
// }

 @PutMapping("/user/{id}/{name}")
 public void updateUser(User user){
  userService.updateUser(user);
 }

 @PutMapping("/updateUser")
 public String updateUser2(@RequestBody User user){
  userService.updateUser(user);
  return "实现数据修改";
 }
}
